package com.dongqi.myblog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongqi.myblog.po.vo.BlogQuery;
import com.dongqi.myblog.po.vo.TypeQuery;
import com.dongqi.myblog.service.TBlogService;
import com.dongqi.myblog.service.TTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dongqi
 * @since 2020-08-03
 */
@Controller
public class TTypeController {

    @Autowired
    private TTypeService typeService;
    @Autowired
    private TBlogService blogService;

    /**
     * 跳转到分类页（默认选中第一个type）
     */
    @GetMapping("/type/{id}")
    public String types(@RequestParam(defaultValue = "1")Long pageNo,
                        @RequestParam(defaultValue = "10") Long pageSize,
                        @PathVariable Long id,
                        Model model){
        //所有分类（降序）
        List<TypeQuery> types = typeService.getAllType();
        if (id<0){
            id = types.get(0).getId();
        }
        //根据分类查询所有blog
        Page<BlogQuery> page = new Page<>(pageNo,pageSize);
        Page<BlogQuery> blogByType = blogService.listType(id, page);
        model.addAttribute("types",types);
        model.addAttribute("page",blogByType);
        model.addAttribute("activeTypeId",id);
        return "types";
    }

}

